/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.border.EtchedBorder;

import soccerscope.model.Player;
import soccerscope.model.SoccerObject;
import soccerscope.util.Color2;

public class ObjectTrainingButton extends JButton {
	private static final long serialVersionUID = -6766831964829889642L;

	private SoccerObject object = null;

	private FontMetrics fm;

	private int objectR;

	private int object2R;

	/**
	 * The constructor creates the graphical layout
	 */
	public ObjectTrainingButton() {
		super();
		setBorder(new EtchedBorder(EtchedBorder.LOWERED));
		setBorderPainted(true);
	}

	public void setObject(SoccerObject obj) {
		this.object = obj;
		this.objectR = (int) (this.object.getSize() * 3.6 * 8.0);
		this.object2R = this.objectR * 2;
	}

	@Override
	public Dimension getPreferredSize() {
		return getMinimumSize();
	}

	@Override
	public Dimension getMinimumSize() {
		return new Dimension(200, 200);
	}

	@Override
	public void paintComponent(Graphics g) {
		if (this.object == null)
			return;
		if (this.fm == null) {
			this.fm = this.getFontMetrics(this.getFont());
		}
		Dimension d = getSize();

		g.setColor(Color2.forestGreen);
		g.fillRect(0, 0, d.width, d.height);

		Font f = g.getFont();
		if (f.isBold()) {
			g.setFont(f.deriveFont(Font.PLAIN));
		}

		int cx = d.width / 2;
		int cy = d.height / 2;
		int x = cx - this.objectR;
		int y = cy - this.objectR;

		if (this.object instanceof Player) {
			Player player = (Player) this.object;
			// fill back
			g.setColor(Color.black);
			g.fillOval(x, y, this.object2R, this.object2R);

			Color drawColor = this.object.getColor();
			// if (player.isKicking()) {
			// g.setColor(drawColor.darker());
			// g.fillOval(x, y, object2R, object2R);
			// g.setColor(Color.yellow);
			// String s = "KICK";
			// g.drawString(s, d.width - (fm.stringWidth(s) + 5),
			// fm.getHeight());
			// } else if (player.isCatching()) {
			// g.setColor(drawColor.darker());
			// g.fillOval(x, y, object2R, object2R);
			// g.setColor(Color.yellow);
			// String s = "CATCH";
			// g.drawString(s, d.width - (fm.stringWidth(s) + 5),
			// fm.getHeight());
			// } else if (player.isTackling()) {
			// g.setColor(drawColor.darker());
			// g.fillOval(x, y, object2R, object2R);
			// g.setColor(Color.yellow);
			// String s = "TACKLE";
			// g.drawString(s, d.width - (fm.stringWidth(s) + 5),
			// fm.getHeight());
			// }

			// body
			g.setColor(drawColor);
			g.drawOval(x, y, this.object2R, this.object2R);

			// angle_body
			g.fillArc(x, y, this.object2R, this.object2R,
					-((int) player.angle + 90), 180);

			// unum
			g.setColor(Color.white);
			g.drawString(Integer.toString(player.unum), 5,
					this.fm.getHeight() + 5);

			// stamina
			// if (player.stamina > Param.STAMINA_MAX * Param.EFFORT_INC_THR) {
			// g.setColor(Color.green);
			// } else if (player.stamina > Param.STAMINA_MAX *
			// Param.EFFORT_DEC_THR) {
			// g.setColor(Color.yellow);
			// } else {
			// g.setColor(Color.red);
			// }
			// String s = Integer.toString(player.stamina);
			// g.drawString(s, d.width - fm.stringWidth(s) - 5, d.height - 5);
		} else {
			// drawball
			g.setColor(this.object.getColor());
			g.fillOval(x, y, this.object2R, this.object2R);
		}
	}

}
